// miniprogram/pages/home/register/AssoRegister.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showTopTips: false,
    msg: "正在加载",
    colleges:[],
    collegeIndex:0,
    assoNames:[],
    assoNameIndex:0,
    assoPickers:[],
    ObjectArray:[
      {
        college:"请选择",
        id:0,
        assoNames:["请选择"]
      },
      {
        college:"医药信息工程学院",
        id:1,
        assoNames:["医药信息工程学院团委学生会","信工辩论队"]
      }
    ],
    accounts: ["请选择","医药信息工程学院", "药学院", "中药学院"],
    accountIndex: 0,
    schoolName:null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var schoolName = app.globalData.studentInfo.schoolName
    wx.request({
      url: app.globalData.url + "/asso/getAssoPicker",
      data: {
        schoolName:schoolName
      },
      success:res =>{
        var assoPickers = res.data.assoPickers
        console.log(assoPickers)
        this.setData({
          assoPickers : assoPickers
        })
        var objectArray = this.data.assoPickers
        var colleges = []
        for (var i = 0; i < objectArray.length; i++) {
          colleges.push(objectArray[i].college)
        }
        this.setData({
          colleges: colleges,
          assoNames: objectArray[this.data.collegeIndex].assoNames
        })
      }
    })
  },
  bindCollegeChange:function(e){
    this.setData({
      collegeIndex:e.detail.value,
      assoNameIndex:0,
    })
    var objectArray = this.data.assoPickers
    this.setData({
      assoNames:objectArray[this.data.collegeIndex].assoNames
    })
  },
  bindAssoNameChange: function (e) {
    this.setData({
      assoNameIndex:e.detail.value
    })
  },
  formSubmit:function(e){
    var that = this;
    var msg = "";
    let { schoolName, college, assoName, password } = e.detail.value;
    console.log('schoolName::'+schoolName)
    if ( college==0 || !password) {
      if (college==0) {
        msg = msg + "所属学院 "
      }
      if (!password) {
        msg = msg + "密码 "
      }
      this.setData({
        msg: msg + "不能为空"
      });
      this.setData({
        showTopTips: true
      });
      setTimeout(function () {
        that.setData({
          showTopTips: false
        });
      }, 3000);
    } else if (password.length < 6) {
      msg = msg + "密码 "
      this.setData({
        msg: msg + "长度出错，请重新输入"
      });
      this.setData({
        showTopTips: true,
      });
      setTimeout(function () {
        that.setData({
          showTopTips: false
        });
      }, 3000);
    }else{
      wx.request({
        url: app.globalData.url +'/asso/register',
        data:{
          openid:app.globalData.openid,
          uniqueId: app.globalData.uniqueId,
          schoolName:schoolName,
          assoName:this.data.assoNames[assoName],
          password:password
        },
        success:res =>{
          var code = res.data.code;
          if (code == 1) {
            wx.hideLoading()
            wx.showModal({
              title: '提示',
              content: '绑定成功',
              success: function (res) {
                if (res.confirm) {
                  wx.switchTab({
                    url: '../index/index',
                  })
                  console.log('用户点击确定')
                } else {
                  console.log('用户点击取消')
                }
              }
            })
          } else {//绑定失败
            wx.hideLoading()
            wx.showModal({
              title: '提示',
              content: res.data.msg,
              success: function (res) {
                if (res.confirm) {
                  that.setData({
                    inputValue: ""
                  })
                  console.log('用户点击确定')
                } else {
                  console.log('用户点击取消')
                }
              }
            })
          } 
        }
      })
    }
    console.log(this.data.assoNames[e.detail.value.assoName])
    console.log(this.data.colleges[e.detail.value.college])
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      schoolName:app.globalData.studentInfo.schoolName
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})